<template>
  <ul class="app-header-nav">
    <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li v-for="item in list" :key="item.id" @mousemove="$store.commit('category/show',item)"  @mouseleave="$store.commit('category/hide',item)">
      <RouterLink :to="`/category/${item.id}`" @click="$store.commit('category/hide',item)">{{item.name}}</RouterLink>
      <div class="layer" :class="{open:item.open}">
        <ul>
          <li v-for="item2 in item.children" :key="item2.id" @click="$store.commit('category/hide',item)">
            <RouterLink :to="`/category/sub/${item2.id}`" >
              <img :src="item2.picture" alt="">
              <p>{{item2.name}}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script>
import { toRefs } from 'vue'
import { useStore } from 'vuex'
export default {
  name: 'AppHeaderNav',
  setup () {
    //   用ref声明响应式数据，优点：可以把请求回来的数据赋值给 value

    // 实例化store对象
    const store = useStore()
    // 发送getList函数中的异步请求
    store.dispatch('category/getList')
    // 将store.state.category对象中的数据变为响应式数据，并让里面的数据展开使用，如list
    return { ...toRefs(store.state.category) }
  }
}
</script>

<style scoped lang='less'>
.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
      // 子分类
    //   鼠标悬停到一级li 的时候显示子分类
      // > .layer {
      //   height: 132px;
      //   opacity: 1;
      // }
    }
  }
}

// 子分类
.layer {
  &.open{
    height: 132px;
    opacity: 1;
  }
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
//   默认子分类的 layer 布局是隐藏起来的
// 1. 高度为0 并且超出隐藏
  height: 0;
  overflow: hidden;
//   2. 透明度为0
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all .2s .1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
